package com.spider.oss;

import com.spider.entity.CmsCertEntity;

public class OssUtil {

    public static String extractFileNamePart(String fileName) {
        // 计算下划线的数量
        int underlineCount = 0;
        for (int i = 0; i < fileName.length(); i++) {
            if (fileName.charAt(i) == '_') {
                underlineCount++;
            }
        }

        if (underlineCount > 2) {
            // 有超过两个下划线的情况
            int firstUnderlineIndex = fileName.indexOf("_");
            int secondUnderlineIndex = fileName.indexOf("_", firstUnderlineIndex + 1);
            int thirdUnderlineIndex = fileName.indexOf("_", secondUnderlineIndex + 1);

            String targetPart = fileName.substring(secondUnderlineIndex + 1, thirdUnderlineIndex);

            // 查找文件名中最后一个 . 的位置
            int lastDotIndex = fileName.lastIndexOf(".");
            if (lastDotIndex != -1) {
                // 获取文件后缀
                String fileExtension = fileName.substring(lastDotIndex);
                // 拼接提取的部分和文件后缀
                return targetPart + fileExtension;
            }
            return targetPart;
        } else {
            // 下划线不超过两个的情况
            int secondUnderlineIndex = fileName.indexOf("_", fileName.indexOf("_") + 1);
            if (secondUnderlineIndex != -1) {
                return fileName.substring(secondUnderlineIndex + 1);
            }
            return null;
        }
    }

    public static CmsCertEntity convertCmsCert(String fileName) {
        // 计算下划线的数量
        int underlineCount = 0;
        for (int i = 0; i < fileName.length(); i++) {
            if (fileName.charAt(i) == '_') {
                underlineCount++;
            }
        }
        // 如果下划线数量少于 2 个，直接返回 null
        if (underlineCount < 2) {
            return null;
        }
        CmsCertEntity cmsCert = new CmsCertEntity();
        int firstUnderlineIndex = fileName.indexOf("_");
        int secondUnderlineIndex = fileName.indexOf("_", firstUnderlineIndex + 1);
        // 提取 code，即第一个下划线和第二个下划线之间的部分
        cmsCert.setCode(fileName.substring(firstUnderlineIndex + 1, secondUnderlineIndex));
        if (underlineCount > 2) {
            // 有超过两个下划线的情况
            int thirdUnderlineIndex = fileName.indexOf("_", secondUnderlineIndex + 1);
            // 提取 batch，即第二个下划线和第三个下划线之间的部分
            cmsCert.setBatch(fileName.substring(secondUnderlineIndex + 1, thirdUnderlineIndex));
        } else {
            // 只有两个下划线的情况，去掉文件后缀
            int lastDotIndex = fileName.lastIndexOf(".");
            if (lastDotIndex != -1) {
                cmsCert.setBatch(fileName.substring(secondUnderlineIndex + 1, lastDotIndex));
            } else {
                cmsCert.setBatch(fileName.substring(secondUnderlineIndex + 1));
            }
        }
        return cmsCert;
    }

    public static void main(String[] args) {
        String fileName1 = "COA_0001_HUOHAO.txt";
        String fileName2 = "COA_0001_HUOHAO_001.txt";
        String fileName3 = "COA_0001.txt";

        CmsCertEntity cmsCert1 = convertCmsCert(fileName1);
        CmsCertEntity cmsCert2 = convertCmsCert(fileName2);
        CmsCertEntity cmsCert3 = convertCmsCert(fileName3);

        System.out.println("FileName1: " + cmsCert1);
        System.out.println("FileName2: " + cmsCert2);
        System.out.println("FileName3: " + cmsCert3);
    }
}
